package com.liqi.device.service.impl;

import com.liqi.common.constant.UserConstants;
import com.liqi.common.utils.CacheUtils;
import com.liqi.common.utils.DateUtils;
import com.liqi.common.utils.StringUtils;
import com.liqi.device.domain.DvBrand;
import com.liqi.device.domain.DvManufacturer;
import com.liqi.device.mapper.DvBrandMapper;
import com.liqi.device.mapper.DvManufacturerMapper;
import com.liqi.device.service.IDvManufacturerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;


/**
 * 厂家Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-11
 */
@Service
public class DvManufacturerServiceImpl implements IDvManufacturerService
{
    @Autowired
    private DvManufacturerMapper dvManufacturerMapper;

    @Autowired
    private DvBrandMapper dvBrandMapper;


    /**
     * 项目启动时，初始化有效厂家到缓存
     */
    @PostConstruct
    public void init() {
    	loadingDvManufacturerCache();
    }

    /**
     * 查询厂家
     *
     * @param id 厂家主键
     * @return 厂家
     */
    @Override
    public DvManufacturer selectDvManufacturerById(Long id)
    {
        return dvManufacturerMapper.selectDvManufacturerById(id);
    }

    @Override
    public DvManufacturer selectDvManufacturerByCode(String code) {
        return dvManufacturerMapper.selectDvManufacturerByCode(code);
    }

    /**
     * 查询厂家列表
     *
     * @param dvManufacturer 厂家
     * @return 厂家
     */
    @Override
    public List<DvManufacturer> selectDvManufacturerList(DvManufacturer dvManufacturer)
    {
        return dvManufacturerMapper.selectDvManufacturerList(dvManufacturer);
    }

    /**
     * 新增厂家
     *
     * @param dvManufacturer 厂家
     * @return 结果
     */
    @Override
    public int insertDvManufacturer(DvManufacturer dvManufacturer)
    {
        dvManufacturer.setCreateTime(DateUtils.getNowDate());
        return dvManufacturerMapper.insertDvManufacturer(dvManufacturer);
    }

    /**
     * 修改厂家
     *
     * @param dvManufacturer 厂家
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDvManufacturer(DvManufacturer dvManufacturer)
    {
        DvManufacturer oldManu = dvManufacturerMapper.selectDvManufacturerById(dvManufacturer.getId());
        //取得旧的品牌数据，然后更新表dv_brand的品牌信息字段
        List<DvBrand> oldBrand = dvBrandMapper.selectDvBrandByManufacturerCode(oldManu.getCode());

        dvManufacturer.setUpdateTime(DateUtils.getNowDate());
        int d = dvManufacturerMapper.updateDvManufacturer(dvManufacturer);

        if(oldBrand != null && oldBrand.size() > 0){
            oldBrand.forEach(p->{
                p.setManufacturerCode(dvManufacturer.getCode());
                p.setManufacturerName(dvManufacturer.getName());
                dvBrandMapper.updateDvBrand(p);
            });
        }
        return d;
    }

    /**
     * 批量删除厂家
     *
     * @param ids 需要删除的厂家主键
     * @return 结果
     */
    @Override
    public int deleteDvManufacturerByIds(Long[] ids)
    {
        return dvManufacturerMapper.deleteDvManufacturerByIds(ids);
    }

    /**
     * 删除厂家信息
     *
     * @param id 厂家主键
     * @return 结果
     */
    @Override
    public int deleteDvManufacturerById(Long id)
    {
        return dvManufacturerMapper.deleteDvManufacturerById(id);
    }

    /**
     * 加载有效厂家缓存数据，用于下拉列表
     */
	@Override
	public void loadingDvManufacturerCache() {
		List<DvManufacturer> manCache = dvManufacturerMapper.selectDvManufacturerList(null);
		CacheUtils.setCache(DvManufacturer.class.getSimpleName(), manCache);
	}

	 /**
     * 重置有效厂家缓存数据
     */
	@Override
	public void resetDvManufacturerCache() {


	}

	/**
     * 	校验编码是否唯一
     *
     * @param manufacture 需校验的
     * @return 结果
     */
	@Override
	public String checkCodeUnique(DvManufacturer manufacture) {
		Long configId = StringUtils.isNull(manufacture.getId()) ? -1L : manufacture.getId();
		DvManufacturer info = dvManufacturerMapper.checkCodeUnique(manufacture.getCode());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != configId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
	}
}
